package controllers

import (
	"fmt"
	"strconv"
	"strings"

	m "myerp/models"
	// "github.com/astaxie/beego"
	pg "myerp/src"
)

type ResController struct {
	CommonController
}

// 资源列表
func (this *ResController) List() {
	iDisplayLength := this.GetString("iDisplayLength", "10")
	iLength, _ := strconv.Atoi(iDisplayLength)
	page, _ := this.GetInt("page", 1)
	res, count := m.GetResources(int64(page), int64(iLength), "Level")

	for _, resource := range res {
		switch resource["Type"] {
		case "0":
			resource["Typename"] = "目录"
		case "1":
			resource["Typename"] = "菜单"
		case "2":
			resource["Typename"] = "按钮"
		}
	}

	this.Data["aaData"] = res
	this.Data["iTotalDisplayRecords"] = count
	this.Data["iTotalRecords"] = iLength
	this.Data["ActionName"] = "资源列表 "
	var pages pg.Page = pg.NewPage(page, iLength, int(count), "/res/list")
	this.Data["page"] = pages.Show()
	this.Layout = "base.html"
	this.TplName = "res/list.html"
}

// 显示单个资源信息
func (this *ResController) Show() {

}

// 添加资源
func (this *ResController) Add() {
	if this.Ctx.Request.Method == "POST" {
		name := this.GetString("name")
		pid, _ := this.GetInt64("pid")
		key := this.GetString("key")
		url := this.GetString("url")
		level, _ := this.GetInt64("level")
		decription := this.GetString("description")
		restype := this.GetString("type")

		_, resource := m.GetResourceByName(name)
		if resource.Id == 0 {
			resource.Name = name
			resource.Pid = pid
			resource.Key = key
			resource.Url = url
			resource.Level = level
			resource.Description = decription
			resource.Type = restype
			group := new(m.Group)
			group.Id = 1
			resource.Group = group

			_, err := m.AddResource(&resource)
			if err != nil {
				this.Ctx.Redirect(302, "/res/add.html")
			}
			this.Ctx.Redirect(302, "/res/list.html")
		} else {
			this.Ctx.Redirect(302, "/res/list.html")
		}

	} else {
		this.Data["ActionName"] = "添加资源"
		_, pRes := m.GetParentResource(0)
		this.Data["pRes"] = pRes
		this.Layout = "base.html"
		this.TplName = "res/add.html"
	}

}

// 更新资源
func (this *ResController) Edit() {

	//fmt.Println("Id is ===", Id)
	// fmt.Println("Name is ===", Name)
	// fmt.Println("Url is ===", Url)
	// fmt.Println("Description is ===", Description)
	Id, _ := this.GetInt64("id")
	if this.Ctx.Request.Method == "POST" {
		Id, _ := this.GetInt64("id")
		Name := this.GetString("name")
		Pid, _ := this.GetInt64("pid")
		Key := this.GetString("key")
		Type := this.GetString("type")
		Url := this.GetString("url")
		Level, _ := this.GetInt64("level")
		Description := this.GetString("description")
		resource := new(m.Resource)
		resource.Id = Id
		resource.Name = Name
		resource.Pid = Pid
		resource.Key = Key
		resource.Url = Url
		resource.Level = Level
		resource.Description = Description
		resource.Type = Type
		_, err := m.UpdateResource(resource)
		if err != nil {
			this.Rsp(false, "更新出现问题："+err.Error())
		} else {
			this.Ctx.Redirect(302, "/res/list")
		}
	}
	resource, _ := m.GetResource(Id)
	fmt.Println(resource)
	_, pRes := m.GetParentResource(0)
	this.Data["ActionName"] = "添加资源 / " + resource.Name
	this.Data["pRes"] = pRes
	this.Data["Resource"] = resource
	this.Layout = "base.html"
	this.TplName = "res/edit.html"
}

// 删除资源
func (this *ResController) Delete() {
	Id := this.GetString("id")
	resId, _ := strconv.Atoi(Id)
	_, err := m.DelResourceById(int64(resId))
	reData := make(map[string]interface{})
	reData["status"] = 0
	reData["msg"] = "删除失败！"
	if err == nil {
		reData["status"] = 1
		reData["msg"] = "删除成功"
	}
	this.Data["json"] = reData
	this.ServeJSON()
}

func (this *ResController) SaveRoleRescours() {
	roleId, _ := this.GetInt64("roleId")
	fid := this.GetString("rescId")
	resList := strings.Split(fid, ",")
	resList = resList[0 : len(resList)-1]
	s := make([]string, 1)
	var isCotaions bool
	fmt.Printf("resList Id is %s\n", resList)
	for _, v := range resList {

		for _, resId := range s {
			if strings.EqualFold(v, resId) {
				isCotaions = true
			}
		}
		if !isCotaions {
			if s[0] == "" {
				s[0] = v
			} else {
				s = append(s, v)
			}

		} else {
			isCotaions = false
		}
	}
	err := m.DelRoleRescoursByRoleId(roleId)
	m.AddRoleRescours(roleId, s)

	if err != nil {
		this.Rsp(false, "fail")
	} else {
		this.Rsp(true, "成功")
	}

}
